﻿using System.Linq;
using CcrFlows.Internal.Stages;
using Microsoft.Ccr.Core;
using NUnit.Framework;

namespace Test.CcrFlows
{
    [TestFixture]
    public class testForkStage
    {
        [Test]
        public void Create_fork()
        {
            StageBase fork = new ForkStage<int, int, string>((i, pi, ps) =>
                                                                 {
                                                                     pi.Post(i + 1);
                                                                     ps.Post(i + "x");
                                                                 });

            int n;
            string t;
            fork.Input.First().Post(99);
            Assert.IsTrue(((Port<int>)fork.Output.ElementAt(0)).WaitFor(500, out n));
            Assert.AreEqual(100, n);
            Assert.IsTrue(((Port<string>)fork.Output.ElementAt(1)).WaitFor(500, out t));
            Assert.AreEqual("99x", t);
        }


        [Test]
        public void Concatenate_fork_to_SimpleStage()
        {
            StageBase simple = new SeqStage<int, int>((i, pi) => pi.Post(i+1));
            StageBase fork = new ForkStage<int, int, string>((i, pi, ps) =>
                                                                {
                                                                    pi.Post(i + 1);
                                                                    ps.Post(i + "x");
                                                                });
            simple.ContinueWith(0, fork);

            int n;
            string t;
            simple.Input.First().Post(99);
            Assert.IsTrue(((Port<int>)fork.Output.ElementAt(0)).WaitFor(500, out n));
            Assert.AreEqual(101, n);
            Assert.IsTrue(((Port<string>)fork.Output.ElementAt(1)).WaitFor(500, out t));
            Assert.AreEqual("100x", t);
        }
    }
}
